// pages/Payment/Payment.js
import config from '../../utils/config'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        infoCardList: [],
        userId: '',
        isover: false,
        pageNum: 1,
        delete: true,
        selected: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this;
        wx.getStorage({
            "key": "userInfo",
            success: (res) => {
                that.setData({
                    userId: res.data.userId
                })
                this.getInfoCard()
            }
        })

    },
    getInfoCard() {
        let that = this;
        wx.request({
            url: `${config.baseUrl}/getInfoCard`,
            method: 'GET',
            data: {
                pageNum: that.data.pageNum,
                userId: that.data.userId
            },
            success: res => {
                wx.stopPullDownRefresh()
                that.setData({
                    pageNum: that.data.pageNum + 1
                })
                console.log('res', res);
                if (res.data.code == 200) {
                    let list = that.data.infoCardList;
                    console.log(res.data.data);
                    list = list.concat(res.data.data)
                    that.setData({
                        infoCardList: list
                    })
                } else {
                    that.setData({
                        isover: true
                    })
                }
            }
        })
    },
    delete() {
        this.setData({
            delete: false
        })
    },
    cancel() {
        this.setData({
            delete: true
        })
    },
    deleteAll() {
        let that=this
        wx.showModal({
            title: '删除',
            content: '确定全部删除吗？',
            success(res) {
                if (res.confirm) {
                    wx.request({
                        url: `${config.baseUrl}/delpublish`,
                        method:'POST',
                        success: (res) => {
                            console.log('delete',res);
                            if (res.data.code == 200) {
                                wx.showToast({
                                    title: res.data.msg
                                })
                                that.onPullDownRefresh()
                            }
                        }
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
        this.cancel()
    },
    selectItem() {
        this.setData({
            selected: !this.data.selected
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.setData({
            pageNum: 1,
            infoCardList: []
        })
        this.getInfoCard()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        console.log('上拉刷新');
        this.getInfoCard()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})